<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>use_future_t::operator()</title>
    <link rel="stylesheet" href="../../../boostbook.css" type="text/css">
    <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
    <link rel="home" href="../../../index.html" title="Asio">
    <link rel="up" href="../use_future_t.html" title="use_future_t">
    <link rel="prev" href="get_allocator.html" title="use_future_t::get_allocator">
    <link rel="next" href="operator_lb__rb_.html" title="use_future_t::operator[]">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%">
    <tr>
        <td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../../asio.png"></td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="get_allocator.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                              href="../use_future_t.html"><img
        src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png"
                                                                                            alt="Home"></a><a
        accesskey="n" href="operator_lb__rb_.html"><img src="../../../next.png" alt="Next"></a>
</div>
<div class="section">
    <div class="titlepage">
        <div>
            <div><h4 class="title">
                <a name="asio.reference.use_future_t.operator_lp__rp_"></a><a class="link" href="operator_lp__rp_.html"
                                                                              title="use_future_t::operator()">use_future_t::operator()</a>
            </h4></div>
        </div>
    </div>
    <p>
        <a class="indexterm" name="asio.indexterm.use_future_t.operator_lp__rp_"></a>
        Wrap
        a function object in a packaged task.
    </p>
    <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">typename</span> <span class="identifier">Function</span><span class="special">&gt;</span>
<span class="identifier">unspecified</span> <span class="keyword">operator</span><span class="special">()(</span>
    <span class="identifier">Function</span> <span class="special">&amp;&amp;</span> <span
                class="identifier">f</span><span class="special">)</span> <span class="keyword">const</span><span
                class="special">;</span>
</pre>
    <p>
        The <code class="computeroutput"><span class="identifier">package</span></code> function is
        used to adapt a function object as a packaged task. When this adapter is
        passed as a completion token to an asynchronous operation, the result of
        the function object is retuned via a std::future.
    </p>
    <h6>
        <a name="asio.reference.use_future_t.operator_lp__rp_.h0"></a>
        <span><a name="asio.reference.use_future_t.operator_lp__rp_.example"></a></span><a class="link"
                                                                                           href="operator_lp__rp_.html#asio.reference.use_future_t.operator_lp__rp_.example">Example</a>
    </h6>
    <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span
            class="identifier">future</span><span class="special">&lt;</span><span class="identifier">std</span><span
            class="special">::</span><span class="identifier">size_t</span><span class="special">&gt;</span> <span
            class="identifier">fut</span> <span class="special">=</span>
  <span class="identifier">my_socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span
                class="special">(</span><span class="identifier">buffer</span><span class="special">,</span>
    <span class="identifier">use_future</span><span class="special">([](</span><span class="identifier">asio</span><span
                class="special">::</span><span class="identifier">error_code</span> <span
                class="identifier">ec</span><span class="special">,</span> <span class="identifier">std</span><span
                class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span><span
                class="special">)</span>
      <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">ec</span> <span class="special">?</span> <span
                class="number">0</span> <span class="special">:</span> <span class="identifier">n</span><span
                class="special">;</span>
      <span class="special">}));</span>
<span class="special">...</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span
                class="identifier">n</span> <span class="special">=</span> <span class="identifier">fut</span><span
                class="special">.</span><span class="identifier">get</span><span class="special">();</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%">
    <tr>
        <td align="left"></td>
        <td align="right">
            <div class="copyright-footer">Copyright © 2003-2022 Christopher M. Kohlhoff<p>
                Distributed under the Boost Software License, Version 1.0. (See accompanying
                file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
            </p>
            </div>
        </td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="get_allocator.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                              href="../use_future_t.html"><img
        src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png"
                                                                                            alt="Home"></a><a
        accesskey="n" href="operator_lb__rb_.html"><img src="../../../next.png" alt="Next"></a>
</div>
</body>
</html>
